﻿var lsInfo = {
  current: 1,
  pageSize: allPageSize,
  data: [],
  totalRow: 0,
  totalPage: 0,
  oneDate: {},
  editState: false
};

document.getElementById('nav').innerHTML = template('navs', navinfo7);
$('#nav li').eq(1).addClass('active');

var m = function (length) {
  var i = 0;
  var l = length;
  var arr = [];
  for (; i < l; i++) {
    arr.push({
      level: '',
      name: '',
      detail: '',
      result: ''
    })
  }
  return arr;
};

var AM = m(3);
var PM = m(5);

var b = new Vue({
  el: '#form',
  data: {
    form: {
      name: '',
      value: '',
      edit: false
    },
    formList: [],
    am: AM,
    pm: PM,
    unfinished: {
      content: '',
      reason: '',
      improve: '',
      etc: '',
      edit: false
    },
    unfinishedList: []
  },
  methods: {
    checkH: function (i) {
      var n;
      if (String(i).length < 2) {
        n = '0' + i;
      } else {
        n = i;
      }

      return n;
    },
    del1: function (i) {
      b.unfinishedList.splice(i, 1);
    },
    edit1: function (i) {
      var content = b.unfinishedList[i].content;
      var reason = b.unfinishedList[i].reason;
      var names = b.unfinishedList[i].names;
      // var etc = b.unfinishedList[i].etc;

      var validator = new Validator();
      validator.add(names, [{
        strategy: 'isNonEmpty',
        errorMsg: '改进措施不能为空'
      }]);

      validator.add(content, [{
        strategy: 'isNonEmpty',
        errorMsg: '工作内容不能为空'
      }]);

      validator.add(reason, [{
        strategy: 'isNonEmpty',
        errorMsg: '未完成原因不能为空'
      }]);

      var errmsg = validator.start();
      if (errmsg) {
        alert(errmsg);
        return false;
      }

      b.unfinishedList[i].edit = !b.unfinishedList[i].edit;
    },
    add1: function () {
      var content = b.unfinished.content;
      var reason = b.unfinished.reason;
      var names = b.unfinished.name;

      var validator = new Validator();

      validator.add(names, [{
        strategy: 'isNonEmpty',
        errorMsg: '未完成项目不能为空'
      }]);

      validator.add(content, [{
        strategy: 'isNonEmpty',
        errorMsg: '工作内容不能为空'
      }]);

      validator.add(reason, [{
        strategy: 'isNonEmpty',
        errorMsg: '未完成原因及改进措施不能为空'
      }]);

      var errmsg = validator.start();
      if (errmsg) {
        alert(errmsg);
        return false;
      }

      b.unfinishedList.push(b.unfinished);
      resetUnfinished();
    },

    del: function (i) {
      b.formList.splice(i, 1);
    },
    edit: function (i) {
      var name = b.formList[i].name;
      var value = b.formList[i].value;

      var validator = new Validator();
      validator.add(name, [{
        strategy: 'isNonEmpty',
        errorMsg: '项目名称不能为空'
      }]);

      validator.add(value, [{
        strategy: 'isNonEmpty',
        errorMsg: '项目总结不能为空'
      }]);

      var errmsg = validator.start();
      if (errmsg) {
        alert(errmsg);
        return false;
      }

      b.formList[i].edit = !b.formList[i].edit;
    },
    add: function () {
      var name = b.form.name;
      var value = b.form.value;

      var validator = new Validator();
      validator.add(name, [{
        strategy: 'isNonEmpty',
        errorMsg: '项目名称不能为空'
      }]);

      validator.add(value, [{
        strategy: 'isNonEmpty',
        errorMsg: '项目总结不能为空'
      }]);

      var errmsg = validator.start();
      if (errmsg) {
        alert(errmsg);
        return false;
      }

      b.formList.push(b.form);
      resetForm();
    }
  }
});

select2();
function select2() {
  $('#day select').each(function () {
    $(this).select2().next().css('width', '100px');
  });

  $('#noComplete').select2();
  $('#summary').select2();

  $('#day select').on('change', function () {
    var selects = $('#day select');
    var obj = {};
    var selected;
    selectedObj = [];
    selects.each(function () {
      selected = $(this).find(':selected');
      if (selected.val()) {
        obj[selected.val()] = selected.html();
      }
    });
    var options = '<option value="" selected="selected">请选择</option>';
    for (var key in obj) {
      options += '<option value="' + obj[key] + '">' + key + '</option>';
    }
    $('#noComplete').html(options).select2();
    $('#summary').html(options).select2();

    $('#noComplete').on('change', function () {
      var v = $(this).find(':selected').val();
      b.unfinished.name = v;
    });

    $('#summary').on('change', function () {
      var v = $(this).find(':selected').val();
      b.form.name = v;
      // console.log(v)
    });
  });
}

function resetForm() {
  b.form = {
    name: '',
    value: '',
    edit: false
  };
}

function resetUnfinished() {
  b.unfinished = {
    content: '',
    reason: '',
    improve: '',
    etc: ''
  };
}

$('#submit').click(function () {
  var table = $('#day tbody tr');
  var pass = true;
  var otable = [];
  var i = 0;
  var l = table.length;
  for (; i < l; i++) {
    var tr = table[i];
    var length = $(tr).children().length;
    var t = $(tr).find('input:radio').parentsUntil('td').parent().eq(0).prev().children().html();
    if (!t) {
      continue;
    }
    // radio
    var name = $(tr).find('input:radio').attr('name');
    if (name) {

      var checked = $(tr).find('[name="' + name + '"]:checked');
      if (!checked.length) {
        alert(t + '重要等级未选择');
        pass = false;
        break;
      }
    }
    // select
    var selected = $(tr).find('select :selected');
    if (!selected.val()) {
      alert(t + '项目未选择');
      pass = false;
      break;
    }

    var textarea1 = $(tr).children('td').eq(length - 2).find('textarea').val();
    if (!textarea1) {
      alert(t + '具体内容未选择');
      pass = false;
      break;
    }

    var textarea2 = $(tr).children('td').eq(length - 1).find('textarea').val();
    if (!textarea2) {
      alert(t + '输出成果未选择');
      pass = false;
      break;
    }

    otable.push({
      important: checked.val(),
      project: selected.val(),
      detail: textarea1,
      out: textarea2
    })
  }

  if (!pass) {
    return false;
  }
  console.log(otable);

  // 未完成
  var unfinishedList = b.unfinishedList;
  var unfinished = b.unfinished;
  var o = [];
  var dirty = false;

  for (var key in unfinished) {
    if (unfinished[key]) {
      dirty = true;
      break;
    }
  }

  if (dirty) {
    var content = unfinished.content;
    var reason = unfinished.reason;
    var improve = unfinished.improve;

    var validator = new Validator();
    validator.add(content, [{
      strategy: 'isNonEmpty',
      errorMsg: '工作内容不能为空'
    }]);

    validator.add(reason, [{
      strategy: 'isNonEmpty',
      errorMsg: '未完成原因不能为空'
    }]);

    validator.add(improve, [{
      strategy: 'isNonEmpty',
      errorMsg: '改进措施不能为空'
    }]);

    for (var key1 in unfinished) {
      if (unfinished[key1].edit) {
        alert('请修改完毕后提交');
        return false;
      }
    }

    var errmsg = validator.start();
    if (errmsg) {
      alert(errmsg);
      return false;
    }

    for (var key in unfinishedList) {
      o[key] = unfinishedList[key];
    }
    o.push(unfinished);

  } else {
    if (!unfinishedList.length) {
      // alert('请填写总结');
      return false;
    }

    for (var key in unfinishedList) {
      if (unfinishedList[key].edit) {
        alert('请修改完毕后提交');
        return false;
      }
    }

    for (var key in unfinishedList) {
      o[key] = unfinishedList[key];
    }
  }

  // 总结
  var list = b.formList;
  var form = b.form;
  var o2 = [];

  if (form.name || form.value) {
    var name = b.form.name;
    var value = b.form.value;

    var validator = new Validator();
    validator.add(name, [{
      strategy: 'isNonEmpty',
      errorMsg: '项目名称不能为空'
    }]);

    validator.add(value, [{
      strategy: 'isNonEmpty',
      errorMsg: '项目总结不能为空'
    }]);

    for (var key in list) {
      if (list[key].edit) {
        alert('请修改完毕后提交');
        return false;
      }
    }

    var errmsg = validator.start();
    if (errmsg) {
      alert(errmsg);
      return false;
    }

    for (var key in list) {
      o2[key] = list[key];
    }
    o2.push(form);

  } else {
    if (!list.length) {
      alert('请填写总结');
      return false;
    }

    for (var key in list) {
      if (list[key].edit) {
        alert('请修改完毕后提交');
        return false;
      }
    }

    for (var key in list) {
      o2[key] = list[key];
    }
  }

  console.log(o);
  console.log(o2);
  resetForm();
  resetUnfinished();
  b.formList = [];
  b.unfinishedList = [];
});